package com.mizhiquan.controller.front;

import com.github.pagehelper.PageInfo;
import com.mizhiquan.common.Const;
import com.mizhiquan.common.ResponseCode;
import com.mizhiquan.common.ServerResponse;
import com.mizhiquan.pojo.Circle;
import com.mizhiquan.pojo.User;
import com.mizhiquan.service.ICircleService;
import com.mizhiquan.vo.UserCirclesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zd on 2017/7/6 0006.
 */
@Controller
@RequestMapping("/circle/")
public class CircleController {

    @Autowired
    private ICircleService iCircleService;

    @RequestMapping(value = "get_circle_info.do",method = {RequestMethod.GET})
    @ResponseBody
    public ServerResponse<Circle> getCircleInfo(String circleId){
        return iCircleService.getCircleInfo(circleId);
    }

    @RequestMapping(value = "get_ask_list.do",method = {RequestMethod.GET})
    @ResponseBody
    //@RequestParam可以指定参数与前端的数据绑定规则，同时可以设置参数是否是必须传值，设置参数默认值等
    public ServerResponse<PageInfo> getAskList(@RequestParam(value = "circleId")String circleId,
                                               @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "15") int pageSize,
                                               @RequestParam(value = "orderBy",defaultValue = "") String orderBy){
        return iCircleService.getAskList(circleId, pageNum, pageSize, orderBy);
    }

    @RequestMapping(value = "get_user_circles.do",method = {RequestMethod.GET})
    @ResponseBody
    public ServerResponse<List<UserCirclesVo>> getUserCircles(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDsc());
        }
        return iCircleService.getUserCircles(user.getUserid());
    }

    @RequestMapping(value = "search_circle.do",method = {RequestMethod.GET})
    @ResponseBody
    public ServerResponse<PageInfo> searchCircles(@RequestParam(value = "keyword") String keyword,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                  @RequestParam(value = "pageSize",defaultValue = "15") int pageSize){
        return iCircleService.searchCircles(keyword, pageNum, pageSize);
    }


}
